package main;

import java.util.HashMap;

import org.encog.Encog;
import org.encog.EncogError;

import configs.Config;
import algorithm.NeuroGA;
import util.MyRandom;
import jmetal.core.Algorithm;
import jmetal.core.Operator;
import jmetal.core.Problem;
import jmetal.core.SolutionSet;
import jmetal.operators.crossover.CrossoverFactory;
import jmetal.operators.mutation.MutationFactory;
import jmetal.operators.selection.SelectionFactory;
import jmetal.qualityIndicator.QualityIndicator;
import jmetal.util.JMException;

public class ExecuteNeuroGA {

	public static void main(String[] args) {
		
		Algorithm algorithm = null;
		try {
			
			Config.LoadConfigs();
			
			jmetal.util.PseudoRandom.setRandomGenerator(new MyRandom(Config.random.nextInt()));
			

			Problem problem = Config.problem;
			algorithm = Config.algorithm;
			
			Operator crossover;
			Operator mutation;
			Operator selection;
			
			HashMap<String,Object> parameters;
			QualityIndicator indicators = new QualityIndicator(problem, "ParetoFronts/ZDT1.txt");
			
			algorithm.setInputParameter("populationSize", Config.populationSize);
			algorithm.setInputParameter("maxEvaluations", Config.maxEvaluations);
						
			parameters = new HashMap<String,Object>();
			parameters.put("probability", 0.9);
			parameters.put("distributionIndex", 20.0);
			crossover = CrossoverFactory.getCrossoverOperator("SBXCrossover", parameters);			
			
			parameters = new HashMap<String,Object>();
			parameters.put("probability", 1.0/problem.getNumberOfVariables());
			parameters.put("distributionIndex", 20.0);
			mutation = MutationFactory.getMutationOperator("PolynomialMutation", parameters);
			
			parameters = null;
			selection = SelectionFactory.getSelectionOperator("BinaryTournament2", parameters);
			
			algorithm.addOperator("crossover", crossover) ;
			algorithm.addOperator("mutation", mutation) ;
			algorithm.addOperator("selection", selection) ;

			algorithm.setInputParameter("indicators", indicators);
			
			SolutionSet population = algorithm.execute();
			population.printObjectivesToFile(Config.DIR+"ObjectivesGA2");
			population.printVariablesToFile(Config.DIR+"VariablesGA2");			
						
			/*
			SolutionSet populationRNA_RBF = algorithm.getSolutionsFromRNA_RBF(population);
			populationRNA_RBF.printObjectivesToFile("ObjectivesRNA_RBF");
			populationRNA_RBF.printVariablesToFile("VariablesRNA_RBF");		*/
			
			System.out.println("TrueParetoFrontHypervolume: " + indicators.getTrueParetoFrontHypervolume());
			System.out.println("Hypervolume: " + indicators.getHypervolume(population));
			System.out.println("Spread: " + indicators.getSpread(population));
			
			if(algorithm instanceof NeuroGA){
				((NeuroGA)algorithm).finish();
			}
			
			Config.EndConfigs();			
			
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (JMException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		} 

	}

}
